package com.project2.service;

import com.project2.bean.vo.*;
import com.project2.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangminghui
 * @date 2022/8/14
 */
@Service
public class WxCategoryServiceImpl implements WxCategoryService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public DataInWxCategoryIndexVo queryCategoryInfo() {
        // todo: 进来默认是停留在第一条category，要找到
        //  数据库中category排序方式，取第一条数据
        //  排序方式可以确定为是： 相同level下，根据id大小排序

        // todo: 根据level+id排序方式，找到L1 level下，
        //  id最小的那一条数据作为currentCategory
        //  还要执行deleted字段判断
        String firstLevel = "L1";
        List<BeanInCategoryListInDataInWxCategoryIndexVo> beanInCategoryListInDataInWxCategoryIndexVos = marketCategoryMapper.queryL1LevelInfoByLevel(firstLevel);
        ArrayList<BeanInCategoryListInDataInWxCategoryIndexVo> beanInCategoryListInDataInWxCategoryIndexVos1 = new ArrayList<>();
        for (BeanInCategoryListInDataInWxCategoryIndexVo beanInCategoryListInDataInWxCategoryIndexVo : beanInCategoryListInDataInWxCategoryIndexVos) {
            if (beanInCategoryListInDataInWxCategoryIndexVo.getDeleted() == false) {
                beanInCategoryListInDataInWxCategoryIndexVos1.add(beanInCategoryListInDataInWxCategoryIndexVo);
            }
        }
        Integer minId = beanInCategoryListInDataInWxCategoryIndexVos1.get(0).getId();
        for (BeanInCategoryListInDataInWxCategoryIndexVo beanInCategoryListInDataInWxCategoryIndexVo : beanInCategoryListInDataInWxCategoryIndexVos1) {
            if (minId > beanInCategoryListInDataInWxCategoryIndexVo.getId()) {
                minId = beanInCategoryListInDataInWxCategoryIndexVo.getId();
            }
        }
        CurrentCategoryInDataInWxCategoryIndexVo currentCategoryInDataInWxCategoryIndexVo = marketCategoryMapper.queryCurrentCategoryByMinidAndLevel(minId, firstLevel);

        DataInWxCategoryIndexVo dataInWxCategoryIndexVo = new DataInWxCategoryIndexVo();
        dataInWxCategoryIndexVo.setCurrentCategory(currentCategoryInDataInWxCategoryIndexVo);
        dataInWxCategoryIndexVo.setCategoryList(beanInCategoryListInDataInWxCategoryIndexVos1);

        // todo: queryCurrentSubCategoryListByPidAndLevel
        String secondLevel = "L2";
        List<BeanInCurrentSubCategoryInDataInWxCategoryIndexVo> beanInCurrentSubCategoryInDataInWxCategoryIndexVos = marketCategoryMapper.queryCurrentSubCategoryListByPidAndLevel(
                currentCategoryInDataInWxCategoryIndexVo.getId(), secondLevel);
        dataInWxCategoryIndexVo.setCurrentSubCategory(beanInCurrentSubCategoryInDataInWxCategoryIndexVos);
        return dataInWxCategoryIndexVo;
    }

    @Override
    public DataInWxCategoryCurrentVo queryCurrentCategoryChildListByPid(Integer id) {
        String firstLevel = "L1";
        CurrentCategoryInDataInWxCategoryIndexVo currentCategoryInDataInWxCategoryIndexVo = marketCategoryMapper.queryCurrentCategoryByMinidAndLevel(id, firstLevel);
        String secondLevel = "L2";
        List<BeanInCurrentSubCategoryInDataInWxCategoryIndexVo> beanInCurrentSubCategoryInDataInWxCategoryIndexVos = marketCategoryMapper.queryCurrentSubCategoryListByPidAndLevel(id, secondLevel);
        DataInWxCategoryCurrentVo dataInWxCategoryCurrentVo = new DataInWxCategoryCurrentVo();
        dataInWxCategoryCurrentVo.setCurrentCategory(currentCategoryInDataInWxCategoryIndexVo);
        dataInWxCategoryCurrentVo.setCurrentSubCategory(beanInCurrentSubCategoryInDataInWxCategoryIndexVos);
        return dataInWxCategoryCurrentVo;
    }


}
